package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.SystemUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.code;
import cn.com.hfuu.module.operate;
import cn.com.hfuu.module.operategz;
import cn.com.hfuu.module.pick;
import cn.com.hfuu.module.qrcode;
import cn.com.hfuu.module.user;
import cn.com.hfuu.module.warehousetea;
import cn.com.hfuu.module.warehousetealog;
import cn.com.hfuu.module.warehouseteax;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.GzService;
import cn.com.hfuu.service.OperateService;
import cn.com.hfuu.service.QrService;
import cn.com.hfuu.service.TeaService;
import cn.com.hfuu.service.TealogService;
import cn.com.hfuu.service.TeaxService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.service.ZlService;
import cn.com.hfuu.vo.Page;

@Controller
@RequestMapping("tea")  
public class TeaControl {
	
	@Autowired
	TeaService teaservice;
	
	@Autowired
	TealogService tealogService;
	
	@Autowired
	ZlService zlService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	CodeService codeService;
	
	@Autowired
	OperateService operateService;
	
	@Autowired
	TeaxService teaxservice;
	
	@Autowired
	QrService qrservice;
	
	@Autowired
	GzService gzService;
	
	
	
	@RequestMapping("info")
	public String info(Model m){
		return "warehouse/tea/tea";
	}
	@RequestMapping("in")
	public String in(Model m){
		return "warehouse/tea/in";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page list(Integer start, Integer limit, warehousetea tea){
		List<warehousetea> teaxlist=teaservice.selectPage(start,limit,tea,1);
		List<warehousetea> total=teaservice.selectpagetotal(tea,1);
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(teaxlist );
		page.setTotal(total.size() );
		return page;
	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
 
 		List<warehousetea> list = teaservice.selectpagetotal(new warehousetea(),1);
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (warehousetea tea1 : list) {
			Map<String, String> map1 = new HashMap<String, String>();
			
			 
			
			map1.put("编号", tea1.getWarehouseTeaid());
			map1.put("茶名",tea1.getTeaName());
			map1.put("重量(克)",""+tea1.getTeaQuality());
 
			mapList.add(map1);

		}
		String[] title = { "编号", "茶名", "重量(克)" };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "茶叶库存信息");
		return new ModelAndView(ve, map);
	}
	
	@RequestMapping("inlist")
	@ResponseBody
	public Page inlist(Integer start, Integer limit, warehousetea tea){
		List<warehousetea> teaxlist=teaservice.selectPage(start,limit,tea,0);
		List<warehousetea> total=teaservice.selectpagetotal(tea,0);
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(teaxlist );
		page.setTotal(total.size() );
		return page;
	}
	
	@RequestMapping("inproc")
	@ResponseBody
	public String inproc(String id, HttpServletRequest req){	
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		SystemUtil sys = new SystemUtil();
		UUIdUtil idu = new UUIdUtil();
		warehousetea tea=teaservice.selectbyid(id);
		operate op=operateService.selectbyid(tea.getOperateid());
		operategz gz=gzService.selectbyoperateid(op.getOperateId());
		tea.setState(1);
		if(teaservice.update(tea)>0){
			warehousetealog tealog=new warehousetealog();
			tealog.setWarehouseTealogid(idu.getUuid());
			tealog.setWarehouseTeaquality(gz.getOperateQuality());
			tealog.setWarehouseLoguserid(u.getUserId());
			tealog.setWarehouseTealogtime(sys.getDatetime());
			tealog.setWarehouseTeatype(1);
			tealog.setWarehouseTeaid(tea.getWarehouseTeaid());
			tealogService.insert(tealog);
			qrcode qr=new qrcode();
			qr.setQrId(idu.getUuid());
			qr.setTeaId(tea.getWarehouseTeaid());
			qr.setTeaxId(gz.getOperateTeaxid());
			qr.setCount(0);    
			qr.setOperateId(op.getOperateId());
			warehouseteax teax=teaxservice.selectbyid(gz.getOperateTeaxid());
			qr.setPickId(teax.getWarehousePickid());
			qrservice.insert(qr);
			return "success";
		}
		return id;
	
	}
	
	@RequestMapping("out")
	public ModelAndView out(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("warehouse/tea/out");
		List<warehousetea> tealist=teaservice.selectall();
		mv.addObject("tea", tealist);
		return mv;
	}
	
	@RequestMapping("outproc")
	@ResponseBody
	public String outproc(warehousetea tea,HttpServletRequest req){
		HttpSession s=req.getSession();
		user u=(user) s.getAttribute("user");
		warehousetea teaold=teaservice.selectbyid(tea.getWarehouseTeaid());
		float total=teaold.getTeaQuality()-tea.getTeaQuality();
		UUIdUtil idu=new UUIdUtil();
		SystemUtil sys=new SystemUtil();
		if(total<0){
			return "error";
		}else{
			//更新库存
			teaold.setTeaQuality(total);
			teaservice.update(teaold);
			//插入日志
			warehousetealog tlog=new warehousetealog();
			tlog.setWarehouseTealogid(idu.getUuid());
			tlog.setWarehouseTeaquality(tea.getTeaQuality());
			tlog.setWarehouseTealogtime(sys.getDatetime());
			tlog.setWarehouseTeatype(0);
			tlog.setWarehouseTeaid(tea.getWarehouseTeaid());
			tlog.setWarehouseLoguserid(u.getUserId());
			
			tealogService.insert(tlog);
			return "success";
			
		}
		
	}
	
	@RequestMapping("zl")
	public ModelAndView zl(String id){
		ModelAndView mv=new ModelAndView();
		List<code> codelist=codeService.selectbyType("08");
		warehousetea tea=teaservice.selectbyid(id);
		if(tea.getTeaZlid()!=null||tea.getTeaZlid()!=""){
			cn.com.hfuu.module.zl zl=zlService.selectbyid(tea.getTeaZlid());
			zl.setUser(userService.selectuserbyid(zl.getZlUserid()));
			for (code code : codelist) {
				if(code.getCodeValue()==zl.getZlResult()){
					zl.setResult(code);
				}
				System.out.println(zl.toString());
			}
			mv.addObject("tea", tea);
			mv.addObject("zl", zl);
		}
		mv.setViewName("print/zltea");
		return mv;
	}
	
	@RequestMapping("qr")
	public String qr(String id,Model m){
		m.addAttribute("id", id);
		return "print/qr";
		
	}
	
	
}
